package com.sali.design_pattern.h_proxy_pattern.lesson_03_01;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description: JDK动态代理
 * @date: 2023/3/19 21:43.
 * @version: 1.0
 */
public class LogProxy implements InvocationHandler {

    private Object target;

    public Object getProxy(Object target) {
        this.target = target;
        Class<?> aClass = target.getClass();
        return Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("买票...");
        Object invoke = method.invoke(target, args);
        System.out.println("打扫卫生...");
        return invoke;
    }
}
